package com.ayuku.commons.ui.users;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;

import com.ayuku.commons.AYConst;
import com.ayuku.commons.ListFormMaster;
import com.ayuku.commons.Util;
import com.ayuku.commons.business.user.UserImpl;
import com.ayuku.retail.persistence.model.AppUsers;

/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public class ListUser extends ListFormMaster {
	private FormUser frm;

	public ListUser(Shell parent, int style) {
		super(parent, style);
	}

	@Override
	public void showEditForm() {
		TableItem[] selection = tableData.getSelection();

		if ((selection != null) && (selection.length > 0)) {
			TableItem itm = (TableItem) selection[0];
			frm = new FormUser(getDialogShell(), SWT.NULL, AYConst.MODE_VIEW);
			frm.setFormKey(itm.getText(0));
			frm.setFuncId(getFuncId());
			frm.open();
		} else {
			Util.showMsgBox(AYConst.FAIL_VIEW, AYConst.MSG_WARN,
					getDialogShell());
		}
	}

	@Override
	public Integer doDelete(List<String> lst) throws Exception {
		return UserImpl.delete(lst);
	}

	@Override
	public void loadSearchData() throws Exception {
		populateTable("%" + getFSearch().getText() + "%");
	}

	@Override
	public void loadAllData() throws Exception {
		populateTable("%%");
	}

	@Override
	public void showAddForm() {
		frm = new FormUser(getDialogShell(), SWT.NULL, AYConst.MODE_ADD);
		frm.setFuncId(getFuncId());
		frm.open();
	}

	@Override
	public void populateTable(String arg) throws Exception {
		tableData.removeAll();
		List<AppUsers> lst = UserImpl.getList(arg);

		tableData.setItemCount(lst.size());
		data = new Object[lst.size()][];
		for (int i = 0; i < lst.size(); i++) {
			AppUsers pj = lst.get(i);
			data[i] = new Object[] { pj.getUname(), (i + 1), pj.getUname(),
					pj.getName(), pj.getGroupId(), pj.getEmail() };
		}
	}

	@Override
	public void additionalProcess() {
		getDialogShell().setText("Data User");
		getDialogShell().setSize(640, 500);
		Util.setCenter(getDialogShell());
	}

	@Override
	public void printPreview() {
	}

	@Override
	public String[] getColumCaption() {
		final String[] titles = { "", "No.", "Username", "Nama", "Group",
				"Email" };
		return titles;
	}

	@Override
	public int[] getColumWidth() {
		int[] i = {0,40, 100, 200, 150, 160 };
		return i;
	}

	@Override
	protected void fillTable(Object[] obj, TableItem item) {
		item.setText(0, Util.nullToString(obj[0]));
		item.setText(1, Util.nullToString(obj[1]));
		item.setText(2, Util.nullToString(obj[2]));
		item.setText(3, Util.nullToString(obj[3]));
		item.setText(4, Util.nullToString(obj[4]));
		item.setText(5, Util.nullToString(obj[5]));
	}

}
